package com.heima.wemedia.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

//1.直接创建一个新线程池，并取名，在@Async方法处写入线程池名
//2.重写@Async内部的线程池
@Configuration
public class AsyncConfig implements AsyncConfigurer {

    //第一种方式(Bean的名字是必须的)
    @Bean("myPool")
    public Executor ThreadPool(){
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setCorePoolSize(Runtime.getRuntime().availableProcessors());//自己电脑的核数
        pool.setMaxPoolSize(50);
        pool.setQueueCapacity(2);
        pool.setThreadNamePrefix("dyz-pool-");
        return pool;
    }

    //第二种方式
    @Override
    @Bean
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setCorePoolSize(Runtime.getRuntime().availableProcessors());//自己电脑的核数
        pool.setMaxPoolSize(50);
        pool.setQueueCapacity(2);
        pool.setThreadNamePrefix("dyz-pool-");
        return pool;
    }
}
